package com.agent.web;

import com.agent.entity.Knowledge;
import com.agent.page.EasyuiPage;
import com.agent.service.IKnowledgeService;
import com.agent.shiro.ShiroUser;
import com.agent.util.CommonUtils;
import com.agent.util.MyPropertiesConfigurer;
import com.agent.util.ResponseData;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Date;
import java.util.Map;

import static com.agent.util.ResponseData.getSuccessResult;


@Controller
@RequestMapping("/knowledge")
public class KnowledgeController {
	private static Logger log = LoggerFactory.getLogger(KnowledgeController.class);
	@Autowired
	private IKnowledgeService knowledgeService;

	@RequestMapping(method = RequestMethod.GET)
	public String toHtml(){
		return "knowledge/knowledge";
	}
	
	@RequestMapping(value = "/list",method = RequestMethod.GET)
	@ResponseBody
	public EasyuiPage getKnowledgeList(Integer page, Integer rows, Knowledge knowledge) {
		knowledge.setTag(CommonUtils.encodeStr(knowledge.getTag()));
		knowledge.setTitle(CommonUtils.encodeStr(knowledge.getTitle()));
		EasyuiPage<Knowledge> searchKnowledge = knowledgeService
				.searchKnowledge(page,rows,knowledge);
		return searchKnowledge;
		
	}
	
	@RequestMapping(value="/insert",method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> insert(Knowledge knowledge,BindingResult result,HttpServletRequest request) {
		knowledge.setCreateTime(new Date());
		ShiroUser shiroUser = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
		knowledge.setCreator(shiroUser.getUserName());
		knowledgeService.insert(knowledge);
		return getSuccessResult();
	}
	
	@RequestMapping(value="/update", method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> update( Knowledge knowledge,HttpServletRequest request) {
		knowledgeService.update(knowledge);
		return getSuccessResult();
	}
	
	@RequestMapping(value="/delete/{id}")
	@ResponseBody
	public Map<String,Object> delete(@PathVariable("id") String id) {
	  	knowledgeService.delete(id);
		return getSuccessResult();
		  
	}



}
